﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using GeckoPhotos.Service;
using GeckoPhotos.Repository;
using GeckoPhotos.Models;
using MvcContrib;
using GeckoPhotos.Auth;
using System.Web.UI;

namespace GeckoPhotos.Controllers
{
    public class ImageController : Controller
    {

        public class UploadModel
        {
            public string Caption { get; set; }
        }

        private readonly ImageServiceLayer _service;

        public ImageController()
            : this(null)
        {
        }

        public ImageController(ImageServiceLayer service)
        {
            DatabaseEntities context = new DatabaseEntities();
            _service = service ?? new ImageServiceLayer(new EFImageRepository(context), new EFUserRepository(context));
        }

        [HttpGet]
        public ActionResult Index()
        {
            IEnumerable<Image> result = _service.GetAll();
            return View(result);
        }

        [HttpGet]
        public ActionResult View(int id)
        {
            Image image = _service.GetById(id);

            if (image == null)
            {
                return this.RedirectToAction(c => c.Index());
            }

            return View(image);
        }

        [HttpGet]
        [Authorize]
        public ActionResult Upload()
        {
            return View(new UploadModel());
        }

        [HttpPost]
        [ValidateInput(false)]
        [Authorize]
        public ActionResult Upload(UploadModel model)
        {
            HttpPostedFileBase file = Request.Files["Filename"];

            ValidateUpload(model, file);

            if (ModelState.IsValid)
            {
                var bytes = new byte[file.ContentLength];
                file.InputStream.Read(bytes, 0, file.ContentLength);

                string username = User.Identity.Name;

                Image newImage = _service.Insert(MvcApplication.Settings, model.Caption, bytes, username);

                return this.RedirectToAction(c => c.View(newImage.ImageID));
            }
            else
            {
                return View(model);
            }
        }

        protected void ValidateUpload(UploadModel model, HttpPostedFileBase file)
        {
            if (file.ContentLength == 0)
                ModelState.AddModelError("Filename", "Please select the file to upload.");
        }

        [HttpGet]
        public ActionResult Get(int id)
        {
            Image image = _service.GetById(id);
            if (image == null)
                throw new HttpException(404, string.Format("Image #{0} does not exist.", id));

            return new FileContentResult(image.Blob.Bytes, image.Blob.MimeType);
        }

        [HttpGet]
        public ActionResult GetThumbnail(int id)
        {
            Image image = _service.GetById(id);
            if (image == null)
                throw new HttpException(404, string.Format("Image #{0} does not exist.", id));

            return new FileContentResult(image.ThumbnailBlob.Bytes, image.ThumbnailBlob.MimeType);
        }

        [HttpGet]
        [Authorize]
        public ActionResult Delete(int id)
        {
            Image image = _service.GetById(id);
            if (image == null)
                throw new HttpException(404, string.Format("Image #{0} does not exist.", id));

            return View(image);
        }

        [HttpGet]
        [Authorize]
        public ActionResult DeleteConfirmed(int id)
        {
            _service.Delete(id);
            return this.RedirectToAction(c => c.Index());
        }

    }
}
